﻿package com.wandian.view.components.ui
{
	import com.wandian.ApplicationFacade;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.view.mediator.HelpMediator;

	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class LevelManage extends Object
	{
		private var _facade:ApplicationFacade;
		private const _levelManageW:int=90;
		private const _levelW:int=18;
		private var _spriteLevel:MovieClip;
		private var _middle:Boolean;

		public function LevelManage():void
		{
			this._facade=ApplicationFacade.getInstance();
			return;
		} 

		public function updateLevel(mc:MovieClip, levelNum:uint, boolMiddle:Boolean=true):void
		{
			this._spriteLevel=mc;
			this.removeLevel(this._spriteLevel);
			if (this._spriteLevel)
			{
			}
			if (!this._spriteLevel.hasEventListener(MouseEvent.CLICK))
			{
				this._spriteLevel.buttonMode=true;
				this._spriteLevel.addEventListener(MouseEvent.CLICK, this.onShowLevelHandler);
			}
			this._middle=boolMiddle;
			this.contLevel(levelNum);
			return;
		} 

		private function onShowLevelHandler(event:MouseEvent):void
		{
			this._facade.sendNotification(CLZNoticeName.SHOW_HELP, HelpMediator.TYPE_8);
			return;
		} 

		private function contLevel(levelNum:int):void
		{
			var levelOne:Class=null;
			var levelTwo:Class=null;
			var levelThree:Class=null;
			var levelFour:Class=null;
			var levelFive:Class=null;
			var levelSix:Class=null;
			switch (levelNum)
			{
				case 0:
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
				{
					levelOne=GameData.getClass("LevelOne");
					this.showLevelData(levelNum, levelOne);
					break;
				}
				case 6:
				case 7:
				case 8:
				case 9:
				case 10:
				{
					levelTwo=GameData.getClass("LevelTwo");
					this.showLevelData(levelNum - 5, levelTwo);
					break;
				}
				case 11:
				case 12:
				case 13:
				case 14:
				case 15:
				{
					levelThree=GameData.getClass("LevelThree");
					this.showLevelData(levelNum - 10, levelThree);
					break;
				}
				case 16:
				case 17:
				case 18:
				case 19:
				case 20:
				{
					levelFour=GameData.getClass("LevelFour");
					this.showLevelData(levelNum - 15, levelFour);
					break;
				}
				case 21:
				case 22:
				case 23:
				case 24:
				case 25:
				{
					levelFive=GameData.getClass("LevelFive");
					this.showLevelData(levelNum - 20, levelFive);
					break;
				}
				case 26:
				case 27:
				case 28:
				case 29:
				case 30:
				{
					levelSix=GameData.getClass("LevelSix");
					this.showLevelData(levelNum - 25, levelSix);
					break;
				}
				default:
				{
					levelSix=GameData.getClass("LevelSix");
					this.showLevelData(30 - 25, levelSix);
					break;
					break;
				}
			}
			return;
		} 

		public function playFlash(num:int=0):void
		{
			var timer:Timer;
			var timerHandler:Function;
			var timerComplete:Function;
			timerHandler=function(event:TimerEvent=null):void
			{
				var _loc_3:MovieClip=null;
				if (!_spriteLevel)
				{
					return;
				}
				var childNum:*=_spriteLevel.numChildren;
				while (childNum)
				{

					childNum=childNum - 1;
					_loc_3=_spriteLevel.getChildAt(childNum) as MovieClip;
					if (_loc_3 != null)
					{
						_loc_3.play();
					}
				}
				return;
			} 
			;
			timerComplete=function(event:TimerEvent):void
			{
				removeTimer();
				return;
			} 
			;
			var removeTimer:*=function():void
			{
				if (timer != null)
				{
					timer.removeEventListener(TimerEvent.TIMER, timerHandler);
					timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
					timer=null;
				}
				return;
			} 
			;
			var playNum:*=num;
			removeTimer();
			if (playNum != 1)
			{
				if (playNum == 0)
				{
					timer=new Timer(2000);
				}
				else
				{
					timer=new Timer(2000, (playNum - 1));
				}
				timer.addEventListener(TimerEvent.TIMER, timerHandler);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
				timer.start();
			}
			timerHandler();
			return;
		} 

		public function stopFlash():void
		{
			var mc:MovieClip=null;
			var num:*=this._spriteLevel.numChildren;
			while (num)
			{

				num=num - 1;
				mc=this._spriteLevel.getChildAt(num) as MovieClip;
				if (mc != null)
				{
					mc.gotoAndStop(1);
				}
			}
			return;
		} 

		private function showLevelData(total:int, levelName:Class):void
		{
			var mc:MovieClip=null;
			var levels:Array=[];
			var count:int=1;
			while (count <= total)
			{

				mc=new levelName;
				mc.gotoAndStop(1);
				levels.push(mc);
				count=count + 1;
			}
			this.addLevel(levels);
			return;
		} 

		private function addLevel(param1:Array):void
		{
			var _loc_3:int=0;
			var _loc_2:*=param1.length;
			if (this._middle)
			{
				_loc_3=(this._levelManageW - this._levelW * _loc_2) / 2;
			}
			else
			{
				_loc_3=0;
			}
			var _loc_4:int=0;
			while (_loc_4 < param1.length)
			{

				this._spriteLevel.addChild(param1[_loc_4]);
				param1[_loc_4].x=_loc_3;
				_loc_3=_loc_3 + this._levelW;
				_loc_4=_loc_4 + 1;
			}
			return;
		} 

		public function removeLevel(param1:MovieClip):void
		{
			var _loc_2:*=undefined;
			param1.removeEventListener(MouseEvent.CLICK, this.onShowLevelHandler);
			while (param1.numChildren > 0)
			{

				_loc_2=param1.removeChildAt(0);
				_loc_2=null;
			}
			return;
		} 

	}
}
